package com.mybatis.boost.generator.plugin;

import com.mybatis.boost.generator.plugin.adapter.MapperPluginAdapter;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

/**
 * @author jearton
 * @since 2016/11/17
 */
public class LombokPlugin extends MapperPluginAdapter {
    private final FullyQualifiedJavaType lombokData = new FullyQualifiedJavaType("lombok.Data");
    private final FullyQualifiedJavaType lombokNoArgs = new FullyQualifiedJavaType("lombok.NoArgsConstructor");
    private final FullyQualifiedJavaType lombokAllArgs = new FullyQualifiedJavaType("lombok.AllArgsConstructor");

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return false;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        this._makeLombok(topLevelClass);
        return true;
    }

    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        this._makeLombok(topLevelClass);
        return true;
    }

    @Override
    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        this._makeLombok(topLevelClass);
        return true;
    }

    private void _makeLombok(TopLevelClass topLevelClass) {
        topLevelClass.addImportedType(this.lombokData);
        topLevelClass.addImportedType(this.lombokNoArgs);
        topLevelClass.addImportedType(this.lombokAllArgs);
        topLevelClass.addAnnotation("@Data");
        topLevelClass.addAnnotation("@NoArgsConstructor");
        topLevelClass.addAnnotation("@AllArgsConstructor");
    }
}